<template>
<!-- <data-type v-bind:resourcecategories="resourcecategories"></data-type> -->
  <div class="resource-list">
    <el-card class="box-card">
      <!-- 头部结构 -->
      <div slot="header" class="clearfix">
        <el-form :inline="true"
          :model="form"
          class="demo-form-inline"
          ref="form">
          <el-form-item label="资源名称" prop="name">
              <el-input v-model="form.name" placeholder="资源名称"
              clearable ></el-input>
          </el-form-item>
          <el-form-item label="资源路径" prop="url">
            <el-input v-model="form.url" placeholder="资源路径" clearable ></el-input>
          </el-form-item>
          <el-form-item label="资源分类" prop="categoryId">
              <el-select v-model="form.categoryId" placeholder="资源分类" clearable>
                <!-- 请求接口进行下拉菜单项设置 -->
                <!-- 进行数据遍历操作 -->
                <el-option
                v-for="item in resourcecategories"
                :key="item.id"
                :label="item.name"
                :value="item.id"></el-option>
                <!-- <el-option label="区域二" value="beijing"></el-option> -->
              </el-select>
          </el-form-item>
          <el-form-item>
              <el-button
                @click="onReset"
                style="margin-right:10px">重置
              </el-button>
              <el-button type="primary"
              @click="onSubmit" :disabled="isLoading">
              查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 功能过渡部分 -->
        <el-form>
          <el-form-item>
            <el-button
              style="margin-right:10px"
              @click="handleAdd">添加资源
            </el-button>
            <el-button
            @click="$router.push({name: 'resource-category'})">
            资源分类</el-button>
          </el-form-item>
        </el-form>
        <!-- 添加资源的对话框结构 -->
      <el-dialog
         :title="isEdit ? '编辑资源' : '添加资源'"
         :visible.sync="dialogVisible"
         width="30%">
         <!-- 引入组件——添加与编辑功能单独封装到组件中 -->
        <create-or-edit
          :resourcecategories="resourcecategories"
          :is-edit="isEdit"
          :resource-id="resourceId"
          @success="handleSuccess"
          @fail="handleFail">
        </create-or-edit>
      </el-dialog>
      <!-- 主体结构 -->
      <el-table
        :data="resources"
        style="width: 100%"
         v-loading="isLoading">
        <el-table-column
          type="index"
          label="编号"
          width="180">
        </el-table-column>
        <el-table-column
          prop="name"
          label="资源名称">
        </el-table-column>
        <el-table-column
          prop="url"
          label="资源路径">
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述">
        </el-table-column>
        <!-- 如果要在当前的功能需要设置过滤器那么得获取到数据，
        获取数据就得要用自定义列模板 的作用域插槽工具 -->
        <el-table-column
          label="添加时间">
          <template slot-scope="scope">
            <span>{{ scope.row.createdTime | dateFormat }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">
              删除
            </el-button>
          </template>
         </el-table-column>
      </el-table>
      <!-- 分页结构 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.current"
        :page-sizes="[10, 15, 20]"
        :page-size="form.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        :disabled="isLoading">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import CreateOrEdit from './CreatOrEdit'
import { getResourcePages, getResourcecategories, deleteResource } from '@/services/resource'
export default {
  name: 'ResourceList',
  components: {
    CreateOrEdit
  },
  data () {
    return {
      form: {
        // 当前显示的页数
        current: 1,
        // 当前显示的数据条数
        size: 10,
        name: '',
        url: '',
        categoryId: ''
      },
      // 数据总条数
      totalCount: 0,
      // 用于存储资源列表数据
      resources: [],
      // 存储资源分类信息
      resourcecategories: [],
      // 用于保存加载状态
      isLoading: false,
      // 控制对话框的展示
      dialogVisible: false,
      // 控制新增对话框的功能状态
      isEdit: false,
      // 存储正在编辑的角色 id
      resourceId: ''
    }
  },
  created () {
    // 加载资源数据
    this.loadResources()
    // 加载资源列表
    this.loadgetResourcecategories()
  },
  methods: {
    handleDelete (rowData) {
      console.log(rowData.id)
      // 删除的确认提示
      this.$confirm('确认删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 进行删除操作发送请求到服务器端进行删除
        // console.log(rowData.id)
        const { data } = await deleteResource(rowData.id)
        // console.log(data)
        if (data.code === '000000') {
          // 更新数据列表
          this.loadResources()
          // 提示信息
          this.$message({
            type: 'success',
            message: '已删除成功!'
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 重置按钮点击操作
    onReset () {
      this.$refs.form.resetFields()
    },
    // 提交筛选功能
    async onSubmit () {
      // 请求数据前先将当前页面指定为第一页
      this.form.current = 1
      this.loadResources()
    },
    // 加载资源列表
    async loadgetResourcecategories () {
      const { data } = await getResourcecategories()
      // console.log(data)
      if (data.code === '000000') {
        this.resourcecategories = data.data
      }
      console.log(this.resourcecategories)
    },
    // 每页显示的条数变化时触发
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.form.size = val
      // 修改了每页显示的条数,当前页面回到初始值 current: 1,
      this.form.current = 1
      this.loadResources()
    },
    // 页数发生变化时触发
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.form.current = val
      this.loadResources()
    },
    // 加载资源数据
    async loadResources () {
      // 开始加载数据
      this.isLoading = true
      const { data } = await getResourcePages(this.form)
      // {current: this.form.current,size: this.form.size}
      // console.log(data)
      if (data.code === '000000') {
        this.resources = data.data.records
        this.totalCount = data.data.total
        // 请求完毕取消加载中状态
        this.isLoading = false
      }
    },
    // 监听子组件的添加状态,成功时触发 handleSuccess
    handleSuccess () {
      // 隐藏对话框
      this.dialogVisible = false
    },
    // 监听子组件的取消状态,点击取消时触发
    handleFail () {
      // 隐藏对话框
      this.dialogVisible = false
    },
    // 点击编辑按钮时弹出编辑角色的表单对话框
    handleEdit (rowData) {
      console.log(rowData.id)
      this.dialogVisible = true
      this.isEdit = true
      // console.log(this.isEdit)
      this.resourceId = rowData.id
      // this.loadgetResourcecategories()
      // this.loadResources()
    },
    // 点击添加资源按钮时弹出编辑角色的表单对话框
    handleAdd () {
      this.dialogVisible = true
      this.isEdit = false
    }
  },
  filters: {
    dateFormat (date) {
      date = new Date(date)
      return `${date.getFullYear()}-
      ${date.getMonth() + 1}-
      ${date.getDate()}-
      ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
    }
  }
}
</script>
<style lang="scss" scoped></style>
